package com.equipment.fkdcn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.EParameter;
import com.equipment.fkdcn.pojo.Scrap;
import com.equipment.fkdcn.service.EparameterService;
import com.equipment.fkdcn.service.ScrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/scr")
public class ScrapController {

    @Autowired
    @Qualifier("scrapService")
    private ScrapService scrapService;


    @Autowired
    @Qualifier("eparameterService")
    private EparameterService eparameterService;


    //查询
    @GetMapping("/scrList")
    @ResponseBody
    public String query(@RequestParam("name") String  name){
        List<Scrap> scraps = scrapService.allQuery();
        ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        ArrayList<JSONObject> jsonObjects2 = new ArrayList<JSONObject>();
        for (Scrap r:scraps){
            JSONObject temp = new JSONObject();
            temp.put("sId",r.getsId());//编号
            temp.put("eName",r.getEpId().geteName());//设备名
            temp.put("model",r.getEpId().getModel());//型号
            temp.put("etype",r.getEpId().getEtype().getType());//类型
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            temp.put("scrapDate",sdf.format(r.getScrapDate()));//报废时间
            temp.put("scrapReason",r.getScrapReason());//报废原因
            temp.put("demolitionPrice",r.getDemolitionPrice());//拆废金
            jsonObjects.add(temp);
        }
        if (name!=null&&!name.equalsIgnoreCase("")){
                for (int i=0;i<jsonObjects.size();i++){//模糊查询实现
                    JSONObject jsonObject = jsonObjects.get(i);
                    Set<String> keySet = jsonObject.keySet();
                    Iterator<String> iterator = keySet.iterator();
                    while (iterator.hasNext()){
                        String key=iterator.next().toString();
                        String temp = jsonObject.getString(key);
                        if (temp.indexOf(name)>=0){
                            jsonObjects2.add(jsonObjects.get(i));
                            break;
                        }
                    }
                }
                jsonObjects=jsonObjects2;
        }

        String json = JSON.toJSONString(jsonObjects);
        return  json;
    }


    //删除功能
    @GetMapping("/del")
    @ResponseBody
    public String del(@RequestParam("param")String data){
        String newData = data.substring(1, data.length() - 1);
        String[] list = newData.split(",");
        //转换为int数组
        boolean flag=true;
        for (int i=0;i<list.length;i++){
            boolean b = scrapService.del(Integer.parseInt(list[i]));
            if (!b){
                flag=false;
                break;
            }
        }
        return  "{\"state\":"+flag+"}";
    }


    //查询设备
    @PostMapping("/queryEp")
    @ResponseBody
    public String queryName(@RequestBody String data){
        System.out.println(data);
        JSONObject jsonObject = JSON.parseObject(data);
        EParameter eParameter = JSON.toJavaObject(jsonObject, EParameter.class);
        List<EParameter> list = eparameterService.vagueQuery(eParameter);
        boolean flag=false;
        int eid=0;
        for (int i=0;i<list.size();i++){
            EParameter temp = list.get(i);
            int id = temp.getEtype().getId();
            if (id==eParameter.getEtype().getId()){
                flag=true;
                eid=temp.getEid();
                break;
            }else{
                eid=0;
            }
        }
        boolean f=false;
        if (list.size()>0&&flag) f=true;
        return  "{\"state\":"+f+",\"eId\":"+eid+"}";
    }






}
